package com.yjps.system.model;

import com.yjps.common.model.BaseModel;

import javax.persistence.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by pcc on 2015/7/12.
 */
@Entity
@Table(name = "t_role")
public class Role extends BaseModel{

    @Column(name = "role_name")
    private String rolename;
    @Column(name = "role_description")
    private String role_description;

    @Column(name = "role_state",columnDefinition="INT default 1")
    private int roleState;

    @OneToMany(targetEntity = Permission.class,fetch=FetchType.EAGER,cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinTable(name = "role_permission", joinColumns = {@JoinColumn(name = "role_id")}, inverseJoinColumns = {@JoinColumn(name = "permission_id")})
    private List<Permission> permissions;



    public int getRoleState() {
        return roleState;
    }

    public void setRoleState(int roleState) {
        this.roleState = roleState;
    }

    @Transient
    private String viewPermissions;

    @Transient
    private List<Permission> allPermissions;


    public String getRolename() {
        return rolename;
    }

    public void setRolename(String rolename) {
        this.rolename = rolename;
    }

    public String getRole_description() {
        return role_description;
    }

    public void setRole_description(String role_description) {
        this.role_description = role_description;
    }



    public List<Permission> getPermissions() {
        return permissions;
    }
    public void setPermissions(List<Permission> permissions) {
        this.permissions = permissions;
    }

    public String getViewPermissions() {
        return viewPermissions;
    }

    public void setViewPermissions(String viewPermissions) {
        this.viewPermissions = viewPermissions;
    }

    public List<Permission> getAllPermissions() {
        return allPermissions;
    }

    public void setAllPermissions(List<Permission> allPermissions) {
        this.allPermissions = allPermissions;
    }


    @Transient
    public Set<String> getPermissionsName(){
        HashSet<String> permissionsNameSet = new HashSet<>();
        for(Permission permission:getPermissions()){
            System.out.println("permission.getPermissionName() >>>>> " +permission.getPermissionName());
            permissionsNameSet.add(permission.getPermissionName());
        }
        return permissionsNameSet;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Role role = (Role) o;

        if (id != role.id) return false;
        if (rolename != null ? !rolename.equals(role.rolename) : role.rolename != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (rolename != null ? rolename.hashCode() : 0);
        return result;
    }
}
